package org.example.backend.demos.web.ManagementModule.service.impl;

import org.example.backend.demos.web.ManagementModule.entity.Project;
import org.example.backend.demos.web.ManagementModule.dao.ProjectDao;
import org.example.backend.demos.web.ManagementModule.service.ProjectService;
import org.example.backend.demos.web.result.PageResult;
import org.example.backend.demos.web.result.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 项目表(Project)表服务实现类
 *
 * @author makejava
 * @since 2025-09-08 14:59:08
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectDao projectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Project queryById(Long id) {
        return this.projectDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param project     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Project> queryByPage_limit(Project project, PageRequest pageRequest) {
        long total = this.projectDao.count(project);
        return null ;
    }
    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 页大小
     * @return 查询结果
     */
    @Override
    public Page<Project> GetProjectByPage(int page, int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by("id").ascending());
        // 获取总记录数
        long total = this.projectDao.count(new Project());
        // 获取当前页数据
        List<Project> projects = projectDao.queryAllByLimit(pageable);

        // 构造并返回 Page 对象
        return new PageImpl<>(projects, pageable, total);
    }
    /**
     * 分页查询
     *
     * @param pages 筛选条件
     * @return 筛选结果
     */
    @Override
    public Pages queryByPage(Pages  pages) {
        long total = this.projectDao.count(new Project());
        List<Project> projects = projectDao.queryAllBy(pages);
        pages.setPageResult(new PageResult(total, projects));
        return pages;

    }

    /**
     * 新增数据
     *
     * @param project 实例对象
     * @return 实例对象
     */
    @Override
    public Project insert(Project project) {
        this.projectDao.insert(project);
        return project;
    }

    /**
     * 修改数据
     *
     * @param project 实例对象
     * @return 实例对象
     */
    @Override
    public Project update(Project project) {
        project.setUpdateTime(new Date());
        this.projectDao.update(project);
        return this.queryById(project.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.projectDao.deleteById(id) > 0;
    }

    /**
     * 查询所有数据
     *
     * @return 实例对象
     */
    @Override
    public List<Project> queryAll() {
        return this.projectDao.queryAll();
    }

    @Override
    public List<Project> searchByKeyword(String keyword) {
        return projectDao.blurAll(keyword);
    }
}
